<template>
    <div>
        <el-dialog title="流转人员" :visible.sync="dialogVisible" width='450px' append-to-body v-dialogDrag
            :close-on-click-modal="false" >
            <div>
                <el-form ref="form" :model="rowdata" label-width="100px" class="personeditform">
                    <el-form-item label="在职状态">
                        <el-select v-model="rowdata.zztx" >
                            <el-option label="在职" value="在职"></el-option>
                            <el-option label="退休" value="退休"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="转至党委" >
                            <el-select v-model="rowdata.dwname" @change="changebanzjianshedw">
                                <el-option v-for="item in alldws" :key="item.name" :label="item.name" :value="item.name"></el-option> 
                            </el-select>
                        </el-form-item>
                        <el-form-item label="转至党支部" >
                            <el-select v-model="rowdata.dzbname" >
                                <el-option v-for="item in activedzbs" :key="item.name" :label="item.name" :value="item.name"></el-option> 
                            </el-select>
                        </el-form-item>
                </el-form>
                <el-row style="position: relative;width: 100%;">
                    <el-button style="margin: 0 auto;display: block;"  :disabled="disabled" type="primary" @click="uploadToServe">确定并保存<i class="el-icon-upload el-icon--right" ></i></el-button>
                </el-row>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    export default {
        data() {
            return { 
                dialogVisible: false, 
                rowdata: {},
                user:{},//当前进入人员管理的登陆信息
                alldws:[],//党内有职务时选择的委员会
                activedzbs:[], //党内有职务时选定委员会时出现的党支部
                rowdataTemp:{},
                disabled:false,

            };
        },
        mounted() {
            //获取所有党委进行选择
            let _this=this;
            this.alldws=[];
            let postdata={
                type:"GetZbById",
                split:JSON.stringify({
                    dwid:-1,
                    dzbid:-1
                })
            }
            this.$Common.AjaxPost(postdata,true)
            .then(function(e){
                for (let index = 0; index < e.childs.length; index++) {
                    const element = e.childs[index];
                    _this.alldws.push({
                        id:element.dwid,
                        name:element.alias
                    })
                }
            })
            .catch(function(reason) {
                console.log('catch:', reason);
            });
        },
        watch: {
            dialogVisible: function (n) {
                if (n == true) {
                    this.user=JSON.parse(Common.GetStorage('user'));//用户的数据存进来
                    this.rowdata={
                        zztx: "在职",//在职状态
                        dwname:'',
                        dzbname:'',
                    }
                    this.activedzbs=[]; //党内有职务时选定委员会时出现的党支部
                }
            },
            "rowdataTemp":{
                handler(){
                    this.rowdata=JSON.parse(JSON.stringify(this.rowdataTemp));
                    let _this=this;
                    setTimeout(function(){
                        _this.GetDzbs( _this.rowdata.dwname);
                    },1000)
                },
                deep:true
            },
        },
        methods: {
            GetDzbs(dwname){
                let _this=this;
                let finddws=this.alldws.filter(function(valuedw){
                    return valuedw.name==dwname;
                })
                let dwid=-1;
                if(finddws.length>0){
                    dwid=finddws[0].id;
                }
                else{
                    return;
                }
                if(dwid>=0){
                    //获取党委下所有党支部
                    this.activedzbs=[];
                    let _this=this;
                    let postdata={
                        type:"GetZbById",
                        split:JSON.stringify({
                            dwid:dwid,
                            dzbid:-1
                        })
                    }
                    this.$Common.AjaxPost(postdata,true)
                    .then(function(e){
                        for (let index = 0; index < e.childs.length; index++) {
                            const element = e.childs[index];
                            _this.activedzbs.push({
                                id:element.dzbid,
                                name:element.alias
                            })
                        }
                    })
                    .catch(function(reason) {
                        console.log('catch:', reason);
                    });
                }
            },
            changebanzjianshedw(value){
                this.GetDzbs(value);
            },
            uploadToServe(){
                let _this=this;
                let newPerson={};
                newPerson.zztx=this.rowdata.zztx;
                let dws=this.alldws.filter(function(dw){
                    return dw.name==_this.rowdata.dwname;
                })
                if(dws.length==0){
                    newPerson.dwid="";
                }
                else{
                    newPerson.dwid=dws[0].id+"";
                }
                newPerson.dwid=dws[0].id+"";
                let dzbs=this.activedzbs.filter(function(dzb){
                    return dzb.name==_this.rowdata.dzbname;
                })
                if(dzbs.length==0){
                    newPerson.dzbid="";
                }
                else{
                    newPerson.dzbid=dzbs[0].id+"";
                }
                newPerson.personID=_this.rowdata.personID
                let Ok_load=true;
                for(var key in newPerson)
                {
                    if(newPerson[key]==""){
                        Ok_load=false;
                    }
                }
                if(Ok_load==false){
                    this.$message("不能有空缺项！");
                    return;
                }
                let postdata={
                    type:"liuzhuanPerson",
                    split:JSON.stringify(newPerson)
                }
                this.disabled=true;
                this.$Common.AjaxPost(postdata,true)
                .then(function(e){
                    _this.$message("操作成功");
                    _this.disabled=false;
                    _this.dialogVisible=false;

                    _this.$emit('editResult');
                },function(){
                     _this.$message("操作失败");
                    _this.disabled=false;
                })
                .catch(function(reason) {
                    console.log('catch:', reason);
                });
            },

        },

    }
</script>
<style>
</style>